package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.PayInfoEntity;
import com.sgb.kjwl.model.entity.transport.TransportOrderEntity;
import com.sgb.kjwl.view.ui.activity.transport.driver.trans.TransportFreightCollectionActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Time:2021/3/9
 * Author:黄淋
 * Description:运费收款
 */
public class TransportCollectionModel extends AndroidViewModel {
    public MutableLiveData<PayInfoEntity> ldTradeNo = new MutableLiveData<>();

    public TransportCollectionModel(@NonNull Application application) {
        super(application);
    }

    public void getTransportCollection(TransportFreightCollectionActivity activity, Map map) {

        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().getShippingOrderDetail(map).enqueue(new Callback<BaseEntity<TransportOrderEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<TransportOrderEntity>> call, Response<BaseEntity<TransportOrderEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null || response.body().getData() == null)
                    return;

                if (response.body().getState().equals("ok")) {
                    activity.finish();
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<TransportOrderEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    /**
     * 支付运费
     *
     * @param orderNo
     */
    public void getPayFreight(Context context, String orderNo) {
        Map map = new HashMap();
        map.put("orderNo", orderNo);
        DialogHelper.showProgressDialog(context, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().payFreight(map).enqueue(new Callback<BaseEntity<PayInfoEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<PayInfoEntity>> call, Response<BaseEntity<PayInfoEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null || response.body().getData() == null)
                    return;

                if (response.body().getState().equals("ok") && response.body().getData().getResult().equals("200")) {
                    ldTradeNo.postValue(response.body().getData());
                } else {
                    MToast.showToast(context, response.body().getData().getMessage());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PayInfoEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void onLinePayMethod(TransportFreightCollectionActivity activity,String orderNo) {
        DialogHelper.showProgressDialog(activity, "请求中...").setCancelable(false);
        Map map = new HashMap();
        map.put("orderNo", orderNo);
        APIManager.getInstance().getDriverAPI().onLinePay(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body() == null || response.body().getState() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    activity.finish();
                } else { DialogHelper.dismissProgressDialog();

                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }

        });
    }
}
